import java.util.Scanner;

import javax.swing.text.html.HTMLDocument.HTMLReader.IsindexAction;


/**
 * This class allow to manipulate the game, include the commands and all strategies and their functions 
 * @author Kao Vang
 *
 */
public class Game {
	
	/*Attributes*/
	private ListLuckyNumbers listNumbers ;
	private Strategy strategies;
	
	
	
	/**
	 * Constructor
	 */
	public Game() {
		this.strategies = new Strategy();
		this.listNumbers = new ListLuckyNumbers(this.strategies);
		
	}
	
	
	/**
	 * The menu to setting all strategies
	 */
	public void strategiesSettings() {
		
		
		@SuppressWarnings("resource")
		Scanner input = new Scanner(System.in);
		String cmd = "";

		do {
			/* Menu */
			System.out
					.println("&&&&&&&&&&&&&&&& Strategies settings &&&&&&&&&&&&&&&&");
			System.out
					.println(" n - Set number of Odd/Even number      ");
			System.out
					.println(" e - Set eliminated numbers             ");
			System.out
					.println(" f - Set favors numbers                 ");
			System.out
					.println(" d - Show all strategies                ");
			System.out
					.println(" i - Initial all strategies             ");
			System.out
					.println(" q - Exit setting                       ");
			System.out
					.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
			System.out
					.print("Type command # ");
			
			/* Read command */
			cmd = input.nextLine();

			switch (cmd) {
			case "n":
				numberOddEvenNumberSetting();

				break;
			case "d":
				showStrategies();

				break;
			case "e":
				setEliminatedNum();

				break;
			case "i":
				//TODO

				break;
			case "f":
				//TODO
				//listNumbers.printLuckyNumbersInfo();

				break;
				
			case "q":
				// Update strategies before leaving
				listNumbers.initial();
				listNumbers.updateLuckyNumbers();
				System.out.println("Update lucky numbers with the strategies modified -> Ok");
				break;
				
			default:
				break;
			}

		} while (cmd.compareTo("q") != 0);
		System.out.println(" -> Exit Strategies Settings ! ");
	}
	

	/**
	 * Show all lucky number present in listLuckyNumbers and its status is True
	 */
	public void showResult() {
		listNumbers.showAllLuckyResultNum();
		strategies.showStrategies();
	}
	
	/**
	 * Show all strategies
	 */
	public void showAllStrategies() {
		strategies.showStrategies();
	}
	
	/**
	 * Show a specific lucky number enter by user
	 */
	public void showSecificLuckyNum() {
		System.out.print("Enter an index : ");
		@SuppressWarnings("resource")
		Scanner input = new Scanner(System.in);
		String cmd = "";
		/* Read command */
		cmd = input.nextLine();
		int val = strToInt(cmd);
		if (val != -1 ){
			if(val < listNumbers.size()){
				listNumbers.showLuckyNumberAtIndex(val);
			}else{
				System.out.println("Out of array!");
			}
		}
	}
	
	/**
	 * Show the number of result
	 */
	public void showNumberResult(){
		System.out.print("The number total of the result is : "+listNumbers.getNumberOfResult()+"\n\n");
	}
	

	/**
	 * Show all lucky numbers present in a interval enter by user
	 */
	public void showNumberFromInterval() {
		System.out.print("Enter an interval example 100-200 : ");
		@SuppressWarnings("resource")
		Scanner input = new Scanner(System.in);
		String cmd = "";
		/* Read command */
		cmd = input.nextLine();
		String[] val = cmd.split("-");
		String val1S = val[0];
		String val2S = val[1];
		int val1, val2;
		val1 = strToInt(val1S);
		val2 = strToInt(val2S);
		if ( val1 != -1 && val2 != -1 ){
			if (val1 < val2){
				if (val1 > 0 || val2 < listNumbers.size() ){
					while (val1 < val2){
						listNumbers.showLuckyNumberAtIndex(val1);
						val1++;
					}
				}else{
					System.out.println("Out of array!");
				}
			}else{
				System.out.println(val1 +" must < " + val2);
			}
		}
	}
	
	/**
	 * Initial the game (Update their status to ACTIVE)
	 */
	public void initialGame(){
		listNumbers.initial();
		strategies.initial();
		System.out.println("Initial game -> OK");
	}
	
	/**
	 * Find a lucky number in list of strategies numbers, enter by user
	 */
	public void findALuckyNum(){
		LuckyNumbers lNum ;
		System.out.print("Enter 5 digits(1 to "+ Constants.NUMBER_DIGIT_OF_FANTASY_FIVE+"). \"example: 3 4 10 32 38\" # ");
		@SuppressWarnings("resource")
		Scanner input = new Scanner(System.in);
		String cmd = "";
		/* Read command */
		cmd = input.nextLine();
		cmd = cmd.replaceAll(" {2,}", " ");		// Replace all space by only 1 space 
		String[] val = cmd.split(" ");
		if(isDigits(val)){
			lNum = new LuckyNumbers(strToInt(val[0]), strToInt(val[1]), strToInt(val[2]), strToInt(val[3]), strToInt(val[4]));
			LuckyNumbers lN = listNumbers.findAluckyNumberActived(lNum);
			if(lN != null){
				System.out.println("["+cmd + "] is fond in : " + lN.getNumber_draw() );
			}else{
				System.out.println("["+cmd + "] is NOT fond.");
			}
		}
	}
	
	/**
	 * Test if all digits pass in parameters are all digits
	 * @param val - The array of digits
	 * @return - True if all string are digit
	 */
	public boolean isDigits(String[] val){
		for (int i=0;i<val.length; i++){
			if(strToInt(val[i]) == -1){
				return false;
			}
		}
		return true;
	}
	
	
	/**
	 * Set the numbers to eliminated, enter by user
	 */
	public void setEliminatedNum(){
		System.out.println("The current eliminated numbers is "+ this.strategies.getNumbersEliminates() );
		System.out.print("Enter the eliminated numbers # ");
		@SuppressWarnings("resource")
		Scanner input = new Scanner(System.in);
		String cmd = "";
		int val_cur;
		/* Read command */
		cmd = input.nextLine();
		cmd = cmd.replaceAll(" {2,}", " ");		// Replace all space by only 1 space 
		String[] val = cmd.split(" ");
		for(int i=0;i<val.length;i++){
			val_cur = strToInt(val[i]);
			if(val_cur != -1 ){
				this.strategies.addNumInEliminatedList(val_cur);
			}
		}
	}
	
	/**
	 * Set the numbers to eliminated, enter by user
	 */
	public void setFavorsNum(){
		System.out.println("The current favors numbers is "+ this.strategies.getNumbersFavors() );
		System.out.print("Enter the favors numbers # ");
		@SuppressWarnings("resource")
		Scanner input = new Scanner(System.in);
		String cmd = "";
		int val_cur;
		/* Read command */
		cmd = input.nextLine();
		cmd = cmd.replaceAll(" {2,}", " ");		// Replace all space by only 1 space 
		String[] val = cmd.split(" ");
		for(int i=0;i<val.length;i++){
			val_cur = strToInt(val[i]);
			if(val_cur != -1 ){
				this.strategies.addNumInFavorsList(val_cur);
			}
		}
	}
	
	/**
	 * Setting of number of Odd/Even number strategy
	 */
	public void numberOddEvenNumberSetting() {
		@SuppressWarnings("resource")
		Scanner input = new Scanner(System.in);
		String cmd = "";
		System.out.print("Enter the number of Odd Number for this simulation: ");
		cmd = input.nextLine();
		int val = strToInt(cmd);
		if ( val != -1 ){
			strategies.setNumOdd( val);
			strategies.setNumEven( 5-val );
		}else{
			System.out.print(cmd + " is not a number, please try again.");
		}
		
		System.out.print("[Odd, Even] -> " + "["+strategies.getNumOdd() +", "+ strategies.getNumEven() +"] -> OK \n\n");
	}
	
	
	/**
	 * Convert a String to digit
	 * @param input - String
	 * @return -Its value or -1
	 */
	public int strToInt( String input ) {
	    try {
	        return Integer.parseInt( input );
	    }
	    catch( Exception e ) {
	        return -1;
	    }
	}
	
	/**
	 * Show all strategies
	 */
	private void showStrategies(){
		this.strategies.showStrategies();
	}
}
